﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CommerceOne.Common
{
    public class UrlBuilderService
    {
        private static UrlBuilderService instance;
        private Dictionary<string, string> routes;

        /// <summary>
        /// Initializes a new instance of the <see cref="UrlBuilderService"/> class.
        /// </summary>
        public UrlBuilderService()
        {
            this.routes = new Dictionary<string, string>();
        }

        /// <summary>
        /// Adds the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void Add(string key, string value)
        {
            if (!routes.ContainsKey(key)) this.routes.Add(key, value);
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static UrlBuilderService Instance
        {
            get
            {
                if (instance == null) instance = new UrlBuilderService();
                return instance;
            }
        }

        /// <summary>
        /// Gets the URL route.
        /// </summary>
        /// <param name="pageType">Type of the page.</param>
        /// <param name="objectId">The object id.</param>
        /// <param name="page">The page.</param>
        /// <param name="objectTitle">The object title.</param>
        /// <param name="queryString">The query string.</param>
        /// <returns></returns>
        public string GetUrlRoute(string pageType, int objectId, int page, string objectTitle, string queryString)
        {
            if (!routes.ContainsKey(pageType)) return string.Empty;

            string routeItem = routes[pageType];

            string keyword = "{" + VariableItem.ObjectId + "}";
            routeItem = routeItem.Replace(keyword, objectId.ToString());

            keyword = "{" + VariableItem.ObjectTitle + "}";
            routeItem = routeItem.Replace(keyword, objectTitle);

            keyword = "{" + VariableItem.PageNumber + "}";
            routeItem = routeItem.Replace(keyword, page.ToString());

            if (!string.IsNullOrEmpty(queryString)) routeItem = routeItem + "?" + queryString;
            return routeItem;
        }

    }
}